package com.appkubes.mall.admin.application.goods.goods.controller;

import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.goods.goods.entity.vo.AssociateVO;
import com.appkubes.mall.admin.application.goods.goods.service.GoodsAssociateService;
import com.appkubes.mall.admin.application.goods.goods.service.impl.GoodsServiceImpl;
import com.appkubes.mall.core.po.MallGoodsAssociate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api("商品组合模块")
@RestController
@RequestMapping("/goods/associate")
public class GoodsAssociateController extends BaseController {

    @Autowired
    GoodsAssociateService goodsAssociateService;

    @Autowired
    GoodsServiceImpl goodsServiceImpl;

    @ApiOperation(value = "获取商品组合列表")
    @PostMapping("/getList")
    public Response getList(@RequestParam(defaultValue = "") String goodKey) {
        return Response.success(goodsAssociateService.getGoodList(goodKey, getAppKey()));
    }

    @ApiOperation(value = "删除组合")
    @PostMapping("/remove")
    public Response remove(@RequestParam(defaultValue = "") String associateKey) {
        goodsAssociateService.getDao().deleteByPrimaryKey(associateKey);
        return Response.success();
    }

    @ApiOperation(value = "添加组合")
    @PostMapping("/addAssociateGood")
    public Response addAssociateGood(@Valid AssociateVO associateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.error("参数错误");
        }
        MallGoodsAssociate combination = new MallGoodsAssociate();
        combination.setAssociateGoodKey(associateVO.getAssociateGoodKey());
        combination.setGoodKey(associateVO.getGoodKey());
        combination.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        combination.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return Response.success(goodsAssociateService.add(combination));
    }

    @ApiOperation(value = "获取未加入组合的商品")
    @PostMapping("/getAssociateGoods")
    public Response getAssociateGoods(@RequestParam(defaultValue = "") String goodKey) {
        List<MallGoodsAssociate> goodsAssociateList = goodsAssociateService.getList(goodKey);

        List<String> inGoodsIds = new ArrayList<>();
        for (MallGoodsAssociate associate : goodsAssociateList) {
            inGoodsIds.add(associate.getAssociateGoodKey());
        }
        System.out.println(inGoodsIds);

        return imgResponse(goodsServiceImpl.getCombinationGoods(inGoodsIds));
    }
}
